﻿<?php

//downErWeiMa($_GET['starTime'],$_GET['endTime'],$_GET['shengFeng']);

ini_set("display_errors", "On");

downExcelAll();

function downExcelAll(){


	// $qure = "SELECT title,start_date,NAME,link_url FROM youjutell WHERE NAME IN ".$_GET['ABku']." AND start_date BETWEEN '".$_GET['starDate']." 00:00:00' AND '".$_GET['endDate']." 23:59:59'";

	// $host='localhost';
	// $user='root';
	// $password='root';
	// $database='weizandata';
	// $link=mysqli_connect($host, $user, $password, $database);
	// $link->query("set names utf8");
	// $result=mysqli_query($link, $qure);
	// $arr=mysqli_fetch_all($result, 1);


	$arr = json_decode(getDateAll($_GET['userLevel']))->data;



	// echo '<pre>';

	// 	var_dump($arr);

	// echo '</pre>';



	// return;
	require_once 'C:\phpstudy_pro\WWW\cscs.qumei.com\weizanZB\PHPExcel-1.8\Classes\PHPExcel.php';

	

	$excelFiled=array("ID","最后处理时间","客服姓名","备注","手机号码","录音链接");

	//var_dump($excelFiled);

	$row=1;

	$objPHPExcel=new PHPExcel();

	$objPHPExcel->getActiveSheet()->fromArray($excelFiled,'A1');

	for($i=0;$i<count($arr);$i++){

		//$disCity=$arr['tell'][$i];

	 	$row+=1;

	 	

	 	$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$arr[$i]->_id);
	 	$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$arr[$i]->updatedAt);
	 	$objPHPExcel->getActiveSheet()->setCellValue('C'.$row,$arr[$i]->remark->list[0]->followUserName);
	 	$objPHPExcel->getActiveSheet()->setCellValue('D'.$row,$arr[$i]->remark->list[0]->remark);
	 	$objPHPExcel->getActiveSheet()->setCellValue('E'.$row,$arr[$i]->mobile);
	 	$objPHPExcel->getActiveSheet()->setCellValue('F'.$row,$arr[$i]->remark->list[1]->luyinOssUrl);

	 	


	}

		$objWriter =PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');

		$nameFile='林西客资表'.date("Ymd");

		// echo __DIR__."\\excel\\".$nameFile.".xlsx";

		$objWriter->save(__DIR__."\\excel\\".$nameFile.".xlsx");  // //excel/二维码.xlsx

		// header("Location: excel\\".$nameFile.".xlsx");

		echo "excel\\".$nameFile.".xlsx";

}


function  php_do_url_POST($url,$params,$headers=[]){ //POST get

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $json_string = curl_exec ( $ch );
        $data = json_decode($json_string, true);
        curl_close ( $ch );
        return $data;
    }

function php_do_Url_GET($url,$params,$header=array()){ //Get get

    $url = "{$url}?" . http_build_query ($params);
    
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
    curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSLVERSION, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
 

    $json_string = curl_exec ( $ch );

    // echo $json_string;

    // $data = json_decode($json_string, true);

    curl_close ( $ch );

    return $json_string;
}

function getDateAll($userLevel){  //获取全部A客数据

	$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

	$filter=["userLevel"=>(int)$userLevel];  //条件
	$options=["sort"=>["updatedAt"=>-1]]; //排序和其它
	// $filter=[];
	// $options=[];
	$query=new MongoDB\Driver\Query($filter,$options);

	$cursor = $manager->executeQuery('runoobdb.byrgPhone',$query);
	$arr = array(); //将所有的数据先取出来
	foreach($cursor as $document){
		array_push($arr,(array)$document);

	}

	return json_encode(

		[	'code'=>0,
			"msg"=>"获取数据成功",
			'count'=>count($arr),
			'data'=>$arr
		],
		JSON_UNESCAPED_UNICODE
	);
}


?>